package com.lry.crawlers.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.video.entity.VideoChapter;
import com.lry.crawlers.video.mapper.VideoChapterMapper;
import com.lry.crawlers.video.service.VideoChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoChapterServiceImpl implements VideoChapterService {

    @Autowired
    VideoChapterMapper videoChapterMapper;


    @Override
    public int setVideoChapter(VideoChapter videoChapter) {
        int insert = videoChapterMapper.insert(videoChapter);
        return insert;
    }

    @Override
    public int setVideoChapters(List<VideoChapter> videoChapters) {
        int i = videoChapterMapper.insertBatchSomeColumn(videoChapters);
        return i;
    }


    @Override
    public List<VideoChapter> selectByVideoId(String videoId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("video_id",videoId);
        List<VideoChapter> list = videoChapterMapper.selectList(wrapper);
        if(list!=null){
            return list;
        }
        return null;
    }

    @Override
    public List<Integer> removeDuplicates(List<String> titles, List<VideoChapter> VideoChapters) {
        List<Integer> duplicates = new ArrayList<>();
        List<String> titlesy = new ArrayList<>();
        for (VideoChapter videoChapter : VideoChapters) {
            titlesy.add(videoChapter.getTitle().trim());
        }
        for(int i = 0;i<titles.size();i++){
            boolean contains = titlesy.contains(titles.get(i));
            if(contains){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }


}

